package org.openharmony.qttool;

import com.intellij.ide.plugins.IdeaPluginDescriptor;
import com.intellij.ide.plugins.PluginManagerCore;
import com.intellij.openapi.extensions.PluginId;
import com.intellij.openapi.project.ProjectManager;
import org.openharmony.qttool.components.QtProjectTypeSelector;
import org.jdom.JDOMException;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;

public class QtProjectGenerator {
    private String saveLocation;

    private String projectName;

    private String bundleName;

    private QtSDK qtSdk;

    private String apiVersion;

    private QtProjectTypeSelector.Type type;

    public QtProjectGenerator() {
    }

    public void setSaveLocation(String saveLocation) {
        this.saveLocation = saveLocation;
    }

    public void setProjectName(String projectName) {
        this.projectName = projectName;
    }

    public void setBundleName(String bundleName) {
        this.bundleName = bundleName;
    }

    public  void setQtSdk(QtSDK qtSdk) {
        this.qtSdk = qtSdk;
    }

    public void setApiVersion(String apiVersion) {
        this.apiVersion = apiVersion;
    }

    public void setType(QtProjectTypeSelector.Type t) {
        this.type = t;
    }

//    拷贝模板并且修改文件内容
    public void generate() {
        copyTemplateFiles(Utils.pluginPath(), projectPath());
        modifyFiles(projectPath());
    }

    public String projectPath() {
        return Paths.get(saveLocation, projectName).toString();
    }

    private void copyTemplateFiles(String pluginPath, String projectPath) {
        try {
            if (this.qtSdk == null) throw new RuntimeException("qtSdk is null");
        } catch (RuntimeException e) {
            Utils.handleException(e);
        }
        // 拷贝模板文件
        Utils.copyDirectory(Paths.get(pluginPath, "template/common").toString(), projectPath);

        // 拷贝Qt中的模板
        Path qtEts = Paths.get(qtSdk.prefix(), "openharmony/qtbase");

        Path target = Paths.get(projectPath, "entry/src/main/ets");
        Utils.copyDirectory(qtEts.toString(), target.toString());

        // 拷贝qml或widget程序代码
        if (type == QtProjectTypeSelector.Type.Widget) {
            Utils.copyDirectory(Paths.get(pluginPath, "template/widget").toString(), projectPath);
        }
        if (type == QtProjectTypeSelector.Type.Quick) {
            Utils.copyDirectory(Paths.get(pluginPath, "template/quick").toString(), projectPath);
        }
    }

    private void modifyFile(String path, String file, HashMap<String, String> replaces) {
        Path fileName = Paths.get(path, file);
        Utils.replaceFileContext(fileName, replaces);
    }

    private void modifyFiles(String projectPath) {
        // 修改CMakeLists.txt
        Path fileName = Paths.get(projectPath, "entry/src/main/cpp/CMakeLists.txt");
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("%MyApplicationName%", projectName);
        modifyFile(projectPath, "entry/src/main/cpp/CMakeLists.txt", hashMap);
        modifyFile(projectPath, "AppScope/resources/base/element/string.json", hashMap);

        // 修改build-profile.json5文件
        hashMap.clear();
        hashMap.put("%QT_PREFIX%", qtSdk.prefix());
        hashMap.put("%ARCH%", qtSdk.getArch());
        modifyFile(projectPath, "entry/build-profile.json5", hashMap);

        // 修改app.json
        hashMap.clear();
        hashMap.put("%BUNDLE_NAME%", bundleName);
        modifyFile(projectPath, "AppScope/app.json5", hashMap);

        // 修改顶层的build-profile.json5文件
        hashMap.clear();
        String apiDir = apiVersion.replaceAll("\\s+", "").toUpperCase();
        hashMap.put("%API%", apiVersion);
        modifyFile(projectPath, "build-profile.json5", hashMap);
    }
}
